package com.lly.songhigh.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.RadioStation;
import com.lly.songhigh.volley.StringRequestUtf;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 * 电台Fragment
 */
public class RadioStationFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioStationRadioGroup;
    private RadioButton radioStationZero;
    private RadioButton radioStationTwo;
    private RadioButton radioStationThree;
    private RadioButton radioStationFour;
    private RadioButton radioStationFive;
    private RadioButton radioStationSix;
    private RadioButton radioStationSeven;
    private VolleySingleton volleySingleton;
    private List<RadioStation.DataEntity> dataEntities;
    private FragmentTransaction transaction;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_radio_station, null);
        initView(view);
        return view;
    }

    // 初始化视图
    private void initView(View view) {
        radioStationRadioGroup = (RadioGroup) view.findViewById(R.id.radio_station_radio_group);
        radioStationZero = (RadioButton) view.findViewById(R.id.radio_station_zero);
        radioStationTwo = (RadioButton) view.findViewById(R.id.radio_station_two);
        radioStationThree = (RadioButton) view.findViewById(R.id.radio_station_three);
        radioStationFour = (RadioButton) view.findViewById(R.id.radio_station_four);
        radioStationFive = (RadioButton) view.findViewById(R.id.radio_station_five);
        radioStationSix = (RadioButton) view.findViewById(R.id.radio_station_six);
        radioStationSeven = (RadioButton) view.findViewById(R.id.radio_station_seven);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        radioStationRadioGroup.setOnCheckedChangeListener(this);
    }

    // 初始化数据
    private void initData() {
        volleySingleton = VolleySingleton.getInstance();
        StringRequestUtf stringRequestUtf = new StringRequestUtf(URL.RADIO_STATION_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RadioStation radioStation = gson.fromJson(response, RadioStation.class);
                dataEntities = radioStation.getData();
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(0).getData())).commitAllowingStateLoss();
                radioStationZero.setText(dataEntities.get(0).getTag_type_name());
                radioStationTwo.setText(dataEntities.get(2).getTag_type_name());
                radioStationThree.setText(dataEntities.get(3).getTag_type_name());
                radioStationFour.setText(dataEntities.get(4).getTag_type_name());
                radioStationFive.setText(dataEntities.get(5).getTag_type_name());
                radioStationSix.setText(dataEntities.get(6).getTag_type_name());
                radioStationSeven.setText(dataEntities.get(7).getTag_type_name());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 点击按钮使用不同的Fragment替换
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radio_station_zero:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(0).getData())).commit();
                break;
            case R.id.radio_station_two:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(2).getData())).commit();
                break;
            case R.id.radio_station_three:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(3).getData())).commit();
                break;
            case R.id.radio_station_four:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(4).getData())).commit();
                break;
            case R.id.radio_station_five:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(5).getData())).commit();
                break;
            case R.id.radio_station_six:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(6).getData())).commit();
                break;
            case R.id.radio_station_seven:
                transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.radio_frame, RadioStationClassifyFragment
                        .newInstance(dataEntities.get(7).getData())).commit();
                break;
        }
    }

}
